#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-12-16 9:53 上午
# @Author  : yangdy
# @File    : 2020-12-16.1686.石子游戏VI
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def wordPattern(self, pattern: str, s: str) -> bool:
        mp, ms = {}, {}
        s_list = s.split(' ')
        if len(pattern) != len(s_list):
            return False
        for i in range(len(pattern)):
            pi, si = pattern[i], s_list[i]
            if si in ms and pi in mp:
                sv, pv = ms[si], mp[pi]
                if sv != pi or pv != si:
                    return False
            elif si in ms or pi in mp:
                return False
            else:
                ms[si], mp[pi] = pi, si
        return True


questions = [
    ('abba', 'dog cat cat dog'),
    ('abba', 'dog cat cat fish'),
    ('aaaa', 'dog cat cat dog'),
    ('abba', 'dog dog dog dog')
]

work(questions, lambda q: Solution().wordPattern(q[0], q[1]))

